package org.example.demoblg.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;


import org.example.demoblg.model.User;
import org.example.demoblg.result.Constant;
import org.example.demoblg.result.Jwt;
import org.example.demoblg.result.Result;
import org.example.demoblg.serviser.ServicerUse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

//@controller+resposebody等于restcontroller
@Controller
@RequestMapping("/user")
@ResponseBody

@Slf4j
public class ContrlollerUser {
    @Autowired
    public ServicerUse servicerUse;

    @RequestMapping("/login")
    public Result login(String userName, String password) {
        log.info("password{}",password);
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return Result.fail(Constant.RESULT_CODE_FAIL, "用户或者密码为空");
        }
        User user = servicerUse.getNameByName(userName);
        if (user == null) {
            return Result.fail(Constant.RESULT_CODE_FAIL, "用户不住在");
        }
        if (!user.getPassword().equals(password)) {
            return Result.fail(Constant.RESULT_CODE_FAIL, "用户或者密码为错误");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userName", user.getUserName());
       map.put("id", user.getId());
      String token = Jwt.encrypt(map);
      log.info("token", token);

//        session.setAttribute(Constant.RESULT, user);            session 存储
        return Result.success(token);
    }

    @RequestMapping("/getName")
    public Result getNameById(Integer id) {
        if (id <= 0) {
            return Result.fail(Constant.RESULT_CODE_FAIL, "不存在");
        }
        User user = servicerUse.getNameById(id);
        if (user == null) {
            return Result.fail(Constant.RESULT_CODE_FAIL, "不存在");
        }
        return Result.success(servicerUse.getNameById(id));
    }

    @RequestMapping("/getAuthorInfo")
    public Result findUserById(HttpServletRequest request) {
        String token = request.getHeader("user_token");
        if(token == null) {
            return null;
        }
        Integer id =Jwt.idbytoken(token);
        System.out.println(id);
        if(id == null) {
            return null;
        }
        return Result.success(servicerUse.getNameById(id));
    }
    @RequestMapping("/enroll")
    public Result enroll(String userName, String password) {
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return null;
        }
        log.info("password{}",password);
        User user = new User();
        user.setUserName(userName);
        user.setPassword(password);

        Integer result=servicerUse.enroll(user);
      if(result==0){
          return null;
      }
      return Result.success("注册成功");
    }
}
